package adhoc.tel.fer.hr;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Class that sends requests to the main server using get or post request.
 */
public class Util {

	/**
	 * Sends request by post method.
	 * @param headerName Identifies request.
	 * @param objectToSend JSON object that will be sent.
	 * @return true if message is successfully sent, false otherwise
	 */
	public static boolean sendToServerByPost(String headerName, JSONObject objectToSend) {

		HttpClient client = new DefaultHttpClient();
		String url = Config.WEB_SERVICE_URL+"";
		HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);

		HttpPost post = new HttpPost(url);

		post.addHeader("DB_update", headerName);
		try {
			StringEntity body = new StringEntity(objectToSend.toString(), "utf-8");
			body.setContentEncoding(new BasicHeader(HTTP.UTF_8,"application/json"));
			// sets the body of the request
			post.setEntity(body);

			client.execute(post);

		} 
		catch (ConnectTimeoutException e){
			return false;
		}
		catch (SocketTimeoutException e) {
			return false;
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		} 
		catch (ClientProtocolException e) {
			e.printStackTrace();
			return false;
		} 
		catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Send request by get method.
	 * @param url URL of main server.
	 * @return Response to get method by main server.
	 */
	public static JSONObject makeGetRequest(String url) {
		
			HttpClient client = new DefaultHttpClient();
			HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
			StringBuffer sb;
			JSONObject friends = null;

			HttpGet get = new HttpGet(url);
			
			try {
				HttpResponse response = client.execute(get);
				
				sb = new StringBuffer();
				String line = "";
				BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
				
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				
				friends = new JSONObject(new String(sb));
			} 
			catch (JSONException e) {
				friends = new JSONObject();
				try {
					friends.put("error", "Error in reading data from server.");
				} catch (JSONException e1) {}
				e.printStackTrace();
				return friends;
			}
			catch (ClientProtocolException e) {
				e.printStackTrace();
				return null;
			} 
			catch (ConnectTimeoutException e){
				friends = new JSONObject();
				try {
					friends.put("error", "Connection to the server cannot be established.");
				} catch (JSONException e1) {}
				e.printStackTrace();
				return friends;
			}
			catch (SocketTimeoutException e) {
				friends = new JSONObject();
				try {
					friends.put("error", "Connection to the server cannot be established.");
				} catch (JSONException e1) {}
				e.printStackTrace();
				return friends;
			}
			catch (IOException e) {
				e.printStackTrace();
				return null;
			} 
			
			
		return friends;
	}
}
